import { ResponsePromiseData } from "@/types"
import { message } from "ant-design-vue"
import minappSdk from "minapp-sdk"

// 表
const searchHistoryTabel = new minappSdk.TableObject("search_history")

// 新增搜索记录
async function addSearchHistory<T>(formState: any): ResponsePromiseData<T> {
  formState.search_query = JSON.stringify(formState.search_query)
  let MyRecord = searchHistoryTabel.create()
  MyRecord.set(formState)
  try {
    let { data } = await MyRecord.save()
    return { code: 0, message: "新增成功", data }
  } catch (error: any) {
    message.error(error.message)
    return Promise.reject(error.message)
  }
}

// 获取搜索历史详情
export async function getSearchHistory<T>(id: string): ResponsePromiseData<T> {
  try {
    let { data } = await searchHistoryTabel.get(id)
    return { code: 0, message: "获取成功", data: data as T }
  } catch (error: any) {
    message.error(error.message)
    return Promise.reject(error.message)
  }
}

export default {
  addSearchHistory,
  getSearchHistory
}
